package cn.hackcloud.mutithread.juc;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * i++原子性问题:i++实际上分为三个步骤，读-改-写 在计算机底层
 * 
 * int i=10 i=i++; int temp=i; i=i+1; i=temp;
 *
 *1.CAS是什么? CompareAndSet
 * 比较和替换
 * unsafe类
 */
public class TestAtomic {

	public static void main(String[] args) {
		AtomicDemo demo = new AtomicDemo();
		for (int i = 0; i < 10; i++) {
			new Thread(demo).start();
		}

	}
}

class AtomicDemo implements Runnable {
	AtomicInteger num = new AtomicInteger(0);

	@Override
	public void run() {

		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(getNum());
	}

	public int getNum() {
		return num.incrementAndGet();
	}
}
